package com.itheima.app.manager;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.db.PageBeanVo;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.sun.prism.shader.AlphaOne_RadialGradient_AlphaTest_Loader;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Component
public class MovementManager {

    @Autowired
    OssTemplate ossTemplate;

    @DubboReference
    MovementService movementService;

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    CommentService commentService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public void saveMovementManager(Movement movement, MultipartFile[] multipartFile) throws IOException {

        User user = UserHolder.get();

        List<String> list = new ArrayList<>();

        for (MultipartFile file : multipartFile) {

            String url = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());

            list.add(url);
        }

        movement.setId(ObjectId.get());

        movement.setUserId(user.getId());

        movement.setCreated(System.currentTimeMillis());

        movement.setState(1);

        movement.setMedias(list);

        movement.setSeeType(1);

        movementService.saveMovement(movement);
    }

    public ResponseEntity findMyMovementByPage(Long userId, Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = movementService.findMyMovementByPage(userId, pageNum, pageNum);

        List<MovementVo> movementVoList = new ArrayList<>();

        List<Movement> items = (List<Movement>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(items)) {

            for (Movement item : items) {

                UserInfo userInfo = userInfoService.findByUserId(item.getUserId());

                MovementVo vo = new MovementVo();

                vo.setUserInfo(userInfo);

                vo.setMovement(item);

                movementVoList.add(vo);
            }
        }

        pageBeanVo.setItems(movementVoList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findFriendMovementByPage(Integer pageNum, Integer pageSize) {

        Long userId = UserHolder.get().getId();

        PageBeanVo pageBeanVo = movementService.findFriendMovementByPage(userId, pageNum, pageSize);

        List<Movement> items = (List<Movement>) pageBeanVo.getItems();

        List<MovementVo> movementVoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(items)) {

            for (Movement item : items) {

                UserInfo userInfo = userInfoService.findByUserId(item.getUserId());

                MovementVo movementVo = new MovementVo();

                movementVo.setUserInfo(userInfo);

                movementVo.setMovement(item);

                movementVoList.add(movementVo);
            }
        }
        pageBeanVo.setItems(movementVoList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findRecommendMovementByPage(Integer pageNum, Integer pageSize) {

        Long userId = UserHolder.get().getId();

        PageBeanVo pageBeanVo = movementService.findRecommendMovementByPage(userId, pageNum, pageSize);

        List<Movement> items = (List<Movement>) pageBeanVo.getItems();

        List<MovementVo> movementVoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(items)) {

            for (Movement item : items) {

                UserInfo userInfo = userInfoService.findByUserId(item.getUserId());

                MovementVo movementVo = new MovementVo();

                movementVo.setUserInfo(userInfo);

                movementVo.setMovement(item);

                movementVoList.add(movementVo);
            }
        }
        pageBeanVo.setItems(movementVoList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity movementLike(String publishId) {

        Long userId = UserHolder.get().getId();

        Movement movement = movementService.findById(new ObjectId(publishId));

        Comment comment = new Comment();

        comment.setCreated(System.currentTimeMillis());

        comment.setPublishId(new ObjectId(publishId));

        comment.setUserId(userId);

        comment.setPublishUserId(movement.getUserId());

        comment.setCommentType(1);

        Integer integer = commentService.saveComment(comment);

        stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.MOVEMENT_LIKE, userId, publishId), "1");

        return ResponseEntity.ok(integer);
    }

    public ResponseEntity movementDislike(String publishId) {

        Long userId = UserHolder.get().getId();

        Integer integer = commentService.removeComment(userId, new ObjectId(publishId), 1);

        stringRedisTemplate.delete(StrUtil.format(ConstantUtil.MOVEMENT_LIKE, userId, publishId));

        return ResponseEntity.ok(integer);
    }

    public ResponseEntity movementLove(String publishId) {
        Movement movement = movementService.findById(new ObjectId(publishId));

        Long userId = UserHolder.get().getId();

        Comment comment = new Comment();

        comment.setPublishUserId(movement.getUserId());

        comment.setPublishId(movement.getId());

        comment.setCreated(System.currentTimeMillis());

        comment.setUserId(userId);

        comment.setCommentType(3);

        Integer loveCount = commentService.saveComment(comment);

        stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.MOVEMENT_LOVE, userId, publishId), "1");

        return ResponseEntity.ok(loveCount);

      /*  // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.查询动态详情
        Movement movement = movementService.findById(new ObjectId(publishId));
        // 3.封装comment对象
        Comment comment = new Comment();
        comment.setCreated(System.currentTimeMillis()); // 喜欢时间
        comment.setPublishId(movement.getId());// 动态id
        comment.setCommentType(3); // 类型-喜欢
        comment.setUserId(userId); // 操作人id
        comment.setPublishUserId(movement.getUserId()); // 动态发布人id
        // 4.调用rpc保存
        Integer loveCount = commentService.saveComment(comment);

        // 5. 向redis中存储喜欢标记   movement_love:{userId}_{publishId} = 1
        stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.MOVEMENT_LOVE, userId, publishId), "1");

        // 6.返回喜欢数量
        return ResponseEntity.ok(loveCount);*/
    }

    public ResponseEntity movementUnlove(String publishId) {

        Long userId = UserHolder.get().getId();

        Integer integer = commentService.removeComment(userId, new ObjectId(publishId), 3);

        stringRedisTemplate.delete(StrUtil.format(ConstantUtil.MOVEMENT_LOVE, userId, publishId));

        return ResponseEntity.ok(integer);
    }

    public ResponseEntity findCommentVoByPage(String publishId, Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(publishId), 2, pageNum, pageSize);

        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        List<CommentVo> commentVos = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(items)) {

            for (Comment item : items) {

                UserInfo userInfo = userInfoService.findByUserId(item.getUserId());

                CommentVo commentVo = new CommentVo();

                commentVo.setId(item.getId().toHexString());

                commentVo.setAvatar(userInfo.getAvatar());

                commentVo.setNickname(userInfo.getNickname());

                commentVo.setCreateDate(DateUtil.formatTime(new Date(item.getCreated())));

                commentVo.setContent(item.getContent());

                commentVos.add(commentVo);
            }
        }
        pageBeanVo.setItems(commentVos);

        return ResponseEntity.ok(pageBeanVo);
    }

    public void movementComment(String publishId, String content) {

        Long userId = UserHolder.get().getId();

        Movement movement = movementService.findById(new ObjectId(publishId));

        Comment comment = new Comment();

        comment.setCreated(System.currentTimeMillis());

        comment.setCommentType(2);

        comment.setUserId(userId);

        comment.setPublishId(movement.getId());

        comment.setPublishUserId(movement.getUserId());

        comment.setContent(content);

        commentService.saveComment(comment);
    }
}
